package com.cn.yunhe.rentcars.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.cn.yunhe.rentcars.captcah.BusiLogUtil;
import com.cn.yunhe.rentcars.mapper.BusiLogMapper;
import com.cn.yunhe.rentcars.mapper.CarMapper;
import com.cn.yunhe.rentcars.mapper.SysDictMapper;
import com.cn.yunhe.rentcars.model.Car;
import com.cn.yunhe.rentcars.model.SysDict;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 字典页面控制器
 * 
 * @author 朱钦明
 *
 */
@RestController
public class SysDictController {
	/**
	 * 注入SysDictMapper对象
	 */
	@Autowired
	private SysDictMapper sysDictMapper;
	
	/**
	 * 注入CarMapper对象
	 */
	@Autowired
	private CarMapper carMapper;
	
	/**
	 * @author 朱钦明添加
	 * 注入日志mapper
	 */
	@Autowired
	private BusiLogMapper busiLogMapper;
	
	/**
	 * 跳转到sysdict页面
	 * @return ModelAndView 跳转到sysdict页面
	 */
	@RequestMapping(value="/sysdict",method=RequestMethod.GET)
	public ModelAndView sysDict(){
		return new ModelAndView("sysdict");
	}
	
	/**
	 * 跳转到sysdictlist页面
	 * @return ModelAndView 跳转到sysdictlist页面
	 */
	@RequestMapping(value="/sysdictlist",method=RequestMethod.GET)
	public ModelAndView sysDictList(){
		
		return new ModelAndView("sysdictlist");
	}
	
	/**
	 * 跳转到sysdictadd页面
	 * @return ModelAndView 跳转到sysdictadd页面
	 */
	@RequestMapping(value="/sysdictadd",method=RequestMethod.GET)
	public ModelAndView sysDictAdd(){
		List<SysDict> sysList = sysDictMapper.selectFidById();
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("sysList",sysList);
		return new ModelAndView("sysdictadd",map);
	}
	
	/**
	 * 跳转到sysdictedit页面
	 * @return ModelAndView 跳转到sysdictedit页面
	 */
	@RequestMapping(value="/sysdictedit",method=RequestMethod.GET)
	public ModelAndView sysDictEdit(@RequestParam String id){
		//根据id查询到指定对象
		SysDict sysDict = sysDictMapper.getSysDictById(id);
		
		//查询出所有父类id为0和1的SysDict对象
		List<SysDict> sysList = sysDictMapper.selectFidById();
		
		//把查询到的信息传送到sysdictedit页面
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("sysDict",sysDict);
		map.put("sysList", sysList);
		return new ModelAndView("sysdictedit",map);
	}
	
	
	/**
	 * 获取请求数据并返回给前台一个json数据
	 * @return 返回一个包含map集合的list集合
	 */
	@RequestMapping(value="/sysdicttree",method=RequestMethod.GET)
	public List<Map<String,String>> getTrees(){
		//获取从数据库读取到的所有数据
		List<SysDict> list = sysDictMapper.getSysDicts();
		//创建一个List集合用于返回给页面
		List<Map<String,String>> jsonList = new ArrayList<Map<String,String>>();
		
		//创建一个Map集合用于存放json数据
		Map<String,String> strMap =null;
		
		for(SysDict sysdict:list){
			strMap = new HashMap<String,String>();
			strMap.put("id", sysdict.getId());
			strMap.put("pId", sysdict.getfId());
			strMap.put("name", sysdict.getName());
			strMap.put("code", sysdict.getCode());
			if(sysdict.getfId().equals("0")){
				strMap.put("open","true");
			}
			jsonList.add(strMap);
		}
		return jsonList;
	}
	
	
	/**
	 * 查询所有数据并返回给sysdictlist页面
	 * @param name 前台页面传入数据name
	 */
	@RequestMapping(value="/sysdicts",method=RequestMethod.POST)
	public Map<String,Object> getSysDictByName(@RequestParam String name,@RequestParam int draw,@RequestParam int start,@RequestParam int length){
		//把查询条件放入map集合
		Map<String,String> map = new HashMap<String,String>();
		map.put("name", name);
		
		int pageNum = (start/length)+1;
		int pageSize = length;
		
		PageHelper.startPage(pageNum, pageSize);
		List<SysDict> list = sysDictMapper.getSysDictByName(map);
		PageInfo<SysDict> page = new PageInfo<SysDict>(list);
		
		Map<String,Object> rsMap = new HashMap<String,Object>();
		rsMap.put("draw", draw);
		rsMap.put("data", page.getList());
		rsMap.put("recordsTotal", page.getTotal());
		rsMap.put("recordsFiltered", page.getTotal());
		return rsMap;
	}
	
	/**
	 * 插入数据到字典信息表
	 * @return void
	 * @param sysDict 获取前台数据
	 */
	@RequestMapping(value="/addsysdict",method=RequestMethod.POST)
	public void insertSysDict(@RequestBody SysDict sysDict,HttpServletRequest request){
		sysDictMapper.insertSysDict(sysDict);
		//添加日志信息
		Map<String,Object> busMap = new HashMap<String,Object>();
		busMap.put("logid", UUID.randomUUID().toString());
		busMap.put("businame", "插入一条字典信息");
		busMap.put("busitype", "插入");
		busMap.put("busiconent","插入一条字典信息");
		BusiLogUtil.addBusiLog(request, busiLogMapper, busMap);
	}
	
	/**
	 * 传入一个id字典编号并把信息跳转到指定页面
	 * @param id 传入一个id字典编号
	 * @return ModelAndView 返回一个ModelAndView对象并跳转到sysdictget页面
	 */
	@RequestMapping(value="/sysdictget",method=RequestMethod.GET)
	public ModelAndView getSysDictById(@RequestParam String id){
		SysDict sysDict = sysDictMapper.getSysDictById(id);
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("sysDict", sysDict);
		map.put("orderNum", sysDict.getOrderNum());
		return new ModelAndView("sysdictget",map);
	}
	
	/**
	 * 修改指定字典信息
	 * @param id 传入字典编号
	 * @param sysDict 传入要修改的对象
	 */
	@RequestMapping(value="/editsysdict/{id}",method=RequestMethod.POST)
	public void editSysDict(@PathVariable("id") String id,@RequestBody SysDict sysDict,HttpServletRequest request){
		sysDict.setId(id);
		sysDict.setAddTime(new Date());
		sysDictMapper.updateSysDict(sysDict);
		//添加日志信息
        Map<String,Object> bus1Map = new HashMap<String,Object>();
        bus1Map.put("logid", UUID.randomUUID().toString());
        bus1Map.put("businame", "根据字典id修改字典信息");
        bus1Map.put("busitype", "修改");
        bus1Map.put("busiconent","根据字典id修改字典信息");
        BusiLogUtil.addBusiLog(request, busiLogMapper, bus1Map);
	}
	
	/**
	 * 根据字典编码查询所有信息
	 * @param code 传入的字典编码
	 * @return ModelAndView 跳转到tree页面
	 */
	@RequestMapping(value="/tree",method=RequestMethod.GET)
	public ModelAndView getCodes(@RequestParam String id){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id", id);
		return new ModelAndView("tree",map);
	}
	
	/**
	 * 根据字典编号删除字典信息
	 * @param id 前台页面传入一个字典id
	 */
	@RequestMapping(value="/deletesysdict",method=RequestMethod.POST)
	public void deleteSysDict(@RequestParam String id,HttpServletRequest request){
		sysDictMapper.deleteSysDict(id);
		//添加日志信息
		Map<String,Object> bus1Map = new HashMap<String,Object>();
		bus1Map.put("logid", UUID.randomUUID().toString());
		bus1Map.put("businame", "根据字典id删除字典信息");
		bus1Map.put("busitype", "删除");
		bus1Map.put("busiconent","根据字典id删除字典信息");
		BusiLogUtil.addBusiLog(request, busiLogMapper, bus1Map);
	}
	
	/**
	 * 查询字典查询车辆信息
	 * @param code 传入的字典编码
	 * @return 返回一个map集合给页面
	 */
	@RequestMapping(value="/getcar",method=RequestMethod.POST)
	public Map<String,Object> getCars(@RequestParam String id,@RequestParam int draw,@RequestParam int start,@RequestParam int length){
				//根据字典id查询字典信息
				SysDict sysDict = sysDictMapper.getSysDictById(id);
				
				//创建一个map集合存储数据
				Map<String,Object> sysMap = new HashMap<String,Object>();
				
				//根据字典编号查询查询父类id然后对应于数据库中的相应字段
				if(sysDict !=null){
					if(sysDict.getfId().equals("2")){
						sysMap.put("carbrand", sysDict.getCode());
					}
					if(sysDict.getfId().equals("3")){
						sysMap.put("carcolor", sysDict.getCode());
					}
					if(sysDict.getfId().equals("4")){
						sysMap.put("cartype", sysDict.getCode());
					}
					if(sysDict.getfId().equals("5")){
						sysMap.put("carbrandtype", sysDict.getCode());
					}
					if(sysDict.getfId().equals("6")){
						sysMap.put("isauto", sysDict.getCode());
					}
					if(sysDict.getfId().equals("7")){
						sysMap.put("enginesize", sysDict.getCode());
					}
					if(sysDict.getfId().equals("8")){
						sysMap.put("seatnum", sysDict.getCode());
					}
					if(sysDict.getfId().equals("9")){
						sysMap.put("facetype", sysDict.getCode());
					}
					if(sysDict.getfId().equals("10")){
						sysMap.put("deptname", sysDict.getCode());
					}
					if(sysDict.getfId().equals("11")){
						sysMap.put("carcity", sysDict.getCode());
					}
					if(sysDict.getfId().equals("12")){
						sysMap.put("carstate", sysDict.getCode());
					}
					if(sysDict.getfId().equals("13")){
						sysMap.put("evaluater", sysDict.getCode());
					}
					if(sysDict.getfId().equals("14")){
						sysMap.put("insurer", sysDict.getCode());
					}
					if(sysDict.getfId().equals("15")){
						sysMap.put("safelevel", sysDict.getCode());
					}
				}
				
				//当前页数和显示信息数
				int pageNum = (start/length)+1;
				int pageSize = length;
				
				PageHelper.startPage(pageNum, pageSize);
				//查询车辆信息
				List<Car> carList = carMapper.getCars(sysMap);
				
				PageInfo<Car> page = new PageInfo<Car>(carList);
				
				Map<String,Object> carMap = new HashMap<String,Object>();
				carMap.put("draw",draw);
				carMap.put("data", page.getList());
				carMap.put("recordsTotal", page.getTotal());
				carMap.put("recordsFiltered", page.getTotal());
				
				return carMap;
	}
	
	/**
	 * 跳转到car-user页面
	 * @return 返回一个ModelAndView对象跳转到car-user页面
	 */
	@RequestMapping(value="/car-user",method=RequestMethod.GET)
	public ModelAndView getCarUser(@RequestParam String id){
		Car car = carMapper.getCarById(id);
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("car",car);
		return new ModelAndView("car-user",map);
	}
}
